package com.fastloan.jn.mgt.customer.controller;

import com.fastloan.jn.mgt.customer.model.JnCustomer;
import com.fastloan.jn.mgt.customer.service.impl.JnCustomerServiceImpl;
import com.fastloan.jn.utils.ExcelUtil;
import com.github.pagehelper.PageInfo;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


/**
 * @author chenkai
 *         createTime 2017-10-2017/10/16 20:39
 */
@RestController
@RequestMapping("/api/web/customer")
public class JnCustomerController extends BaseController<JnCustomer> {
    @Autowired
    private JnCustomerServiceImpl customerService;

    /**
     * 客户分页
     *
     * @return
     */
    @RequestMapping("/pageList")
    public ResultTo pageList() {
        return dataPage();
    }


    /**
     * 客户信息下载
     *
     * @return
     */
    @RequestMapping("/download")
    public ResultTo download(HttpServletResponse response) {
        PageInfo data = (PageInfo) dataPage().get("data");
        return ExcelUtil.download(response, data.getList(), "客户信息");
    }

    /**
     * 按id查询客户信息
     *
     * @param customerId
     * @return
     */
    @GetMapping("/{customerId}")
    public ResultTo getCustomerInfo(@PathVariable String customerId) {
        return new ResultTo().setData(customerService.selectByPrimaryKey(customerId));
    }

    /**
     * 新增客户信息
     *
     * @param customer
     * @return
     */
    @PostMapping
    public ResultTo addCustomerInfo(JnCustomer customer) {
        return customerService.insertSelective(customer) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }

    /**
     * 更新客户信息
     *
     * @param customer
     * @return
     */
    @PutMapping
    public ResultTo updateCustomerInfo(JnCustomer customer) {
        return customerService.updateByPrimaryKeySelective(customer) != 0 ? new ResultTo() : new ResultTo(ResultEnum.OPERATION_FAILED);
    }


    /**
     * 单个客户详细信息
     */
    @GetMapping("/customerDetail/{customerId}")
    public ResultTo customerDetail(@PathVariable("customerId") String customerId) {
        JnCustomer customer = customerService.selectByPrimaryKey(customerId);
        return new ResultTo().setData(customer);
    }

    /**
     * 模型评估
     * @param customerId
     * @param jnCreditBusinessModelStr
     * @param modelType
     * @return
     */
    @PutMapping("/model/{customerId}")
    public ResultTo modelResult(@PathVariable String customerId, String jnCreditBusinessModelStr, int modelType) {
        return customerService.modelResult(customerId, jnCreditBusinessModelStr, modelType);
    }

}
